<p:config xmlns:p="http://www.orbeon.com/oxf/pipeline" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:oxf="http://www.orbeon.com/oxf/processors" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xxforms="http://orbeon.org/oxf/xml/xforms" xmlns:xdb="http://orbeon.org/oxf/xml/xmldb" xmlns:xu="http://www.xmldb.org/xupdate">

	<p:param type="input" name="instance"/>

	<p:processor name="oxf:pipeline">
		<p:input name="config" href="../session.xpl"/>
		<p:output name="data" id="session"/>
	</p:processor>
	
	<p:processor name="oxf:unsafe-xslt">
		<p:input name="data" href="#instance"/>
		<p:input name="session" href="#session"/>
		<p:input name="config">
			<xdb:query xsl:version="2.0" collection="/db/portal">
				<xsl:variable name="cat" select="/submission/categoryId"/>
				<xsl:choose>
					<xsl:when test="$cat = 'null'">
						xquery version "1.0";
						<json>{for $d in /widgetsList/category[not(@id)]/widget[number(visible) != number(<xsl:value-of select="doc('input:session')/session/level"/>) * (-1)] return<object>
								<id>{string($d/@id)}</id>
								<name>{$d/name/text()}</name>
								<visible>{$d/visible/text()}</visible>
								<category>null</category>
							</object>}</json>
					</xsl:when>
					<xsl:otherwise>
						xquery version "1.0";
						<json>{for $d in /widgetsList/category[@id = '<xsl:value-of select="$cat"/>']/widget[number(visible/text()) != number(<xsl:value-of select="doc('input:session')/session/level"/>) * (-1)] return<object>
								<id>{string($d/@id)}</id>
								<name>{$d/name}</name>
								<visible>{$d/visible}</visible>
								<category>
									<xsl:value-of select="$cat"/>
								</category>
							</object>}</json>
					</xsl:otherwise>
				</xsl:choose>

			</xdb:query>
		</p:input>
		<p:output name="data" id="query"/>
	</p:processor>
	
	<p:processor name="oxf:xmldb-query">
		<p:input name="datasource" href="/config/datasource.xml"/>
		<p:input name="query" href="#query"/>
		<p:output name="data" id="widgetCat"/>
	</p:processor>
	
	<p:processor name="oxf:pipeline">
		<p:input name="data" href="#widgetCat"/>
		<p:input name="config" href="utils/json.xpl"/>
	</p:processor>

</p:config>